package org.pjlib.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import org.pjlib.R;

/**
 * Created by ws on 2018/12/19.
 */
public class LSActionBar extends RelativeLayout {
    private String TAG="LSActionBar";
    private View bgView;
    private int mWidth,mHeight,measureHeight;
    private int bgColor;
    private float alpha;
    private boolean isPadding;
    public LSActionBar(Context context) {
        this(context,null);
    }

    public LSActionBar(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public LSActionBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        final TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.LSActionBar, defStyleAttr, 0);
        bgColor=a.getColor(R.styleable.LSActionBar_lsBgColor,0xff4063ca);
        alpha=a.getFloat(R.styleable.LSActionBar_lsAlpha,0);
        isPadding=a.getBoolean(R.styleable.LSActionBar_isPadding,true);
        a.recycle();
    }
    /**
     * @param alpha 0-1 透明度
     * */
    public void setBgAlpha(float alpha){
        this.alpha=alpha;
        if(bgView!=null){
            bgView.setAlpha(alpha);
        }
    }

    @Override
    public void addView(View child) {
        for (int i = 0; i < getChildCount(); i++) {
            if(getChildAt(i) ==bgView){
                break;
            }else {
                removeViewAt(i);
            }
        }
        child.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        super.addView(child);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        addBgView();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        mWidth = MeasureSpec.getSize(widthMeasureSpec);
        measureHeight = MeasureSpec.getSize(heightMeasureSpec);
        mHeight=measureHeight+getStateHeight();
        setMeasuredDimension(mWidth,mHeight);
        addBgView();
    }
    private int getStateHeight(){
        return isPadding? getStatusBarHeight(getContext()):0;
    }
    private void addBgView(){
        if(bgView==null && mHeight>0){
            bgView=new View(getContext());
            bgView.setLayoutParams(new LayoutParams(mWidth,mHeight));
            bgView.setBackgroundColor(bgColor);
            setBgAlpha(alpha);
            addView(bgView,0);
        }
    }
    private  int getStatusBarHeight(Context context) {
        // 获得状态栏高度
        int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
        return context.getResources().getDimensionPixelSize(resourceId);
    }
    public boolean isPadding() {
        return isPadding;
    }

    public void setPadding(boolean padding) {
        isPadding = padding;
        requestLayout();
    }

    public int getBgColor() {
        return bgColor;
    }

    public void setBgColor(int bgColor) {
        this.bgColor = bgColor;
        if(bgView!=null){
            bgView.setBackgroundColor(bgColor);
        }
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
        for (int i = 0; i < getChildCount(); i++) {
            View view=getChildAt(i);
            if(view==bgView){
                view.layout(l,t, mWidth, mHeight);
            }else {
                view.layout(l, t +getStateHeight(), mWidth, mHeight);
            }
        }
    }
}
